<script setup>
import { computed, inject } from 'vue'
import { collapseItemProps, collapseKey } from './props'
defineOptions({
  name: 'LeeCollapseItem',
})
const props = defineProps(collapseItemProps)
const collapseCtx = inject(collapseKey)
const isActive = computed(() => {
  return collapseCtx.activeNames.value.includes(props.name)
})
const handleClickItem = () => {
  if (props.disabled) {
    return
  }
  collapseCtx.handleItemClick(props.name)
}
const transitionEvents = {
  beforeEnter(el) {
    el.style.height = '0px'
    el.style.overflow = 'hidden'
  },
  enter(el) {
    el.style.height = el.scrollHeight + 'px'
  },
  afterEnter(el) {
    el.style.height = ''
    el.style.overflow = ''
  },
  beforeLeave(el) {
    el.style.height = el.scrollHeight + 'px'
    el.style.overflow = 'hidden'
  },
  leave(el) {
    el.style.height = '0px'
  },
  afterLeave(el) {
    el.style.height = ''
    el.style.overflow = ''
  },
}
</script>

<template>
  <div
    class="lee-collapse-item"
    :class="{
      'is-disabled': disabled,
    }"
  >
    <div
      class="lee-collapse-item__header"
      :class="{
        'is-disabled': disabled,
        'is-active': isActive,
      }"
      :id="`item-header-${name}`"
      @click="handleClickItem"
    >
      <slot name="title">{{ title }}</slot>
      <lee-icon icon="angle-right" class="header-angle"></lee-icon>
    </div>
    <Transition name="slide" v-on="transitionEvents">
      <div class="lee-collapse-item__wrapper" v-show="isActive">
        <div class="lee-collapse-item__content" :id="`item-content-${title}`">
          <slot />
        </div>
      </div>
    </Transition>
  </div>
</template>
